package com.hspedu.houserent.view;

import com.hspedu.houserent.domain.House;
import com.hspedu.houserent.service.HouseService;
import com.hspedu.houserent.utils.Utility;

/**
 * 名称HouseView
 * 描述[界面] 显示界面，接收用户输入，调用HouseService完成对房屋信息的各种操作
 *
 * @version 1.0
 * @author:zschun
 * @datatime:2023-02-07 12:10
 */
public class HouseView {

    private boolean loop = true;//控制显示菜单
    private char key = ' ';//接收用户输入
    private HouseService houseService = new HouseService(10);

    //修改界面
    public void changeHouse() {
        System.out.println("\n---------------修改房屋---------------");
        System.out.println("请选择待修改房屋编号(-1退出)：");
        int changeId = Utility.readInt();
        if (changeId == -1) {
            System.out.println("您退出了修改房屋...");
            return;
        }
        houseService.change(changeId);
        System.out.println("---------------修改完成---------------");
    }

    //查找界面
    public void searchHouse() {
        System.out.println("\n---------------查找房屋---------------");
        System.out.println("请输入您要查找的id：");
        int searchId = Utility.readInt();
        houseService.search(searchId);
        System.out.println("---------------查找完成---------------");
    }

    //退出界面
    public  void exitHouse() {
        System.out.println("\n---------------退出房屋---------------");
        char c = Utility.readConfirmSelection();
        if (c == 'Y')  loop = false;
    }

    //删除界面
    public void delHouse() {
        System.out.println("\n---------------删除房屋---------------");
        System.out.println("请选择待删除房屋编号（-1）退出：");
        int delId = Utility.readInt();
        if (delId == -1) {
            System.out.println("您放弃了删除房屋...");
            return;
        } else {
            char choice = Utility.readConfirmSelection();
            if (choice == 'Y') {
                houseService.del(delId);
            } else {
                System.out.println("您放弃了删除房屋...");
            }
        }

        System.out.println("---------------删除完成---------------");
    }

    //接受用户输入
    public void addHouse() {
        System.out.println("\n---------------添加房屋---------------");
        System.out.println("姓名：");
        String name = Utility.readString(10);
        System.out.println("电话：");
        String phone = Utility.readString(15);
        System.out.println("地址：");
        String address = Utility.readString(20);
        System.out.println("月租：");
        int rent = Utility.readInt();
        System.out.println("状态(未出租/已出租)：");
        String state = Utility.readString(10);
        House house = new House(0, name, phone, address, rent, state);
        if (houseService.add(house)) {
            System.out.println("添加房屋信息成功~~~");
        } else {
            System.out.println("添加房屋信息失败！！！");
        }
        System.out.println("---------------添加完成---------------");
    }

    public void listHouses() {
        System.out.println("\n---------------房屋列表---------------");
        System.out.println("编号\t\t房主\t\t电话\t\t地址\t\t月租\t\t状态(未出租/已出租)");
        House[] houses = houseService.list();
        for (int i = 0; i < houseService.list().length; i++) {
            if (houses[i] == null) break;

            System.out.println(houses[i]);
        }
        System.out.println("---------------房屋列表显示完毕---------------");
    }

    //显示主菜单
    public void mainMenu() {
        do {
            System.out.println("\n---------------房屋出租系统---------------");
            System.out.println("\t\t\t1 新 增 房 源");
            System.out.println("\t\t\t2 查 找 房 屋");
            System.out.println("\t\t\t3 删 除 房 屋");
            System.out.println("\t\t\t4 修 改 房 屋 信 息");
            System.out.println("\t\t\t5 房 屋 列 表");
            System.out.println("\t\t\t6 退 出");
            System.out.println("请输入您的选择（1-6）：");

            key = Utility.readChar();
            switch (key) {
                case '1':
                    addHouse();
                    break;
                case '2':
                    searchHouse();
                    break;
                case '3':
                    delHouse();
                    break;
                case '4':
                    changeHouse();
                    break;
                case '5':
                    listHouses();
                    break;
                case '6':
                    exitHouse();
                    break;
            }
        } while (loop);
    }
}
